﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace HAKGERSoft.Synapse.Samples
{
    public static class FileUtil
    {
        public static double[] getInput(string fileName)
        {
            Bitmap image = (Bitmap)Image.FromFile(fileName);
            List<double> list = new List<double>();

            for (int i = 0; i < image.Width; i++)
            {
                for (int j = 0; j < image.Height; j++)
                {
                    list.Add(ReadPixel(image.GetPixel(i,j)));
                }
            }

            return list.ToArray();
        }

        private static double ReadPixel(Color color)
        {
            double colorToGray = (color.R*0.3 + color.G*0.59 + color.B*0.11);
            double ret = (colorToGray/255 * (-1.6)) + 0.8;
            return ret;
        }

        public static TrainingItem getTrainingItem(string fileName)
        {
            double[] image = getInput(fileName);
            string imageName = fileName.Substring(fileName.LastIndexOf("\\") + 1);
            string className = imageName.Split('-')[0];
            int classNumber = int.Parse(className);

            return new TrainingItem(classNumber, image);
        }

        internal static Image getImage(string fileName)
        {
            return Image.FromFile(fileName);
        }

        internal static Image getImageFromDoubles(double[] outputImage)
        {
            int size = (int) Math.Sqrt(outputImage.Length);

            Bitmap image = new Bitmap(size, size);

            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    Color color = ColorFromDouble(outputImage[i*size + j]);
                    image.SetPixel(i,j, color);
                }
            }

            return (Image)image;
        }

        private static Color ColorFromDouble(double input)
        {
            int gray = (int)(((input*(-1)) + 0.8) * 255 / 1.6);
            return Color.FromArgb(gray, gray, gray);
       }

       public static double[][] CreateSomInput(List<TrainingItem> trainingItems, int windowSize, int step)
       {
               double[][] ret;
               List<double[]> vectors = new List<double[]>();
               int pictureSize = (int)Math.Sqrt(trainingItems[0].Image.Length);
               foreach (var item in trainingItems)
               {
                   double[] image = item.Image;
                   for (int l = 0; l < pictureSize - step; l += step)
                   {
                       for (int k = 0; k < pictureSize - step; k+= step)
                       {
                           double[] windowVector = new double[windowSize*windowSize];
                           for (int i = 0; i < windowSize; i++)
                           {
                               for (int j = 0; j < windowSize; j++)
                               {
                                   windowVector[i*windowSize + j] = image[l*k + k + (pictureSize*i + j)];
                               }
                           }
                           vectors.Add(windowVector);
                       }
                   }
               }
               ret = new double[vectors.Count][];
               for (int i = 0; i < vectors.Count; i++)
               {
                   ret[i] = vectors.ElementAt(i);
               }
           //return null;
           return ret;
       }
    }
}
